package com.marisfrolg.config;


import com.marisfrolg.model.MyUserDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    //密码模式才需要配置,认证管理器
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .anyRequest().permitAll()
                .and()
                .formLogin()
                .and()
                .logout();
    }

//    @Bean
//    public UserDetailsService userDetailsService() {
//        return s -> {
////            if ("admin".equals(s)) {
////
////                MyUserDetails user = new MyUserDetails(s, passwordEncoder().encode(s));
////                //根据角色查询权限 to-do
////                user.setPerms("order-createOrder");
////                return user;
////            }
//            if ("user".equals(s)) {
//                MyUserDetails user = new MyUserDetails(s, passwordEncoder().encode(s));
//                //根据角色查询权限 to-do
//                user.setPerms("order-getOrder");
//                user.setUserId(100100001L);
//
//                return user;
//            }
//            if ("admin".equals(s)) {
//                MyUserDetails user = new MyUserDetails(s, passwordEncoder().encode(s));
//                //根据角色查询权限 to-do
//                user.setPerms("order-createOrder");
//                user.setUserId(100100002L);
//                return user;
//            }
//            return null;
//        };
//    }
}